# -*- coding: utf-8 -*-
# 
# plik definiujacy obiekty przechowujace dane

from datetime import date

# collections.OrderedDict jest nowy w Pythonie 2.7, załączony działa w >= 2.4
try:
    from collections import OrderedDict
except ImportError:
    from lib.ordereddict import OrderedDict

class Channel(object):
    """
    definicja kanalu
    """
    def __init__(self, name, id, language=None, icon_url=None, type=None, number=None):
        self.name = name
        self.id = id
        self.language = language
        self.icon_url = icon_url
        self.type = type
        self.number = number

    def __hash__(self):
        return hash(self.id)

    def __cmp__(self, other):
        if self.number is not None and other.number is not None:
            return cmp(self.number, other.number)
        return cmp(self.id, other.id)

class ChannelDict(OrderedDict):
    """
    słownik kanałów {'id': Channel}, zapamiętujący kolejność dodania
    """
    
    parser = None
    date = None
    
    def __init__(self, parser, *args, **kwargs):
        self.parser = parser
        self.date = date.today()
        super(ChannelDict, self).__init__(*args, **kwargs) 
    
    def sorted(self):
        """zwraca nowy obiekt - posortowany słownik"""
        list = self.values()
        list.sort()
        return ChannelDict(self.parser, [(item.id, item) for item in list])

class Event(object):
    """
    wydarzenie (program w telewizji), zawiera dane zakodowane w unicode
    """
    def __init__(self, title, time_start, subtitle=None, original_title=None,
                 year=None, category=None, description=None, time_end=None,
                 categories=None, countries=None, actors=None, directors=None,
                 episode=None, season=None, episode_count=None, runtime=None,
                 properties=None, thumb_url=None, **kwargs):
        
        self.title, self.subtitle = title, subtitle
        self.time_start, self.time_end = time_start, time_end
        self.year, self.category, self.description = year, category, description
        self.categories, self.countries = categories, countries
        self.actors, self.directors = actors, directors
        self.episode, self.season = episode, season
        self.episode_count, self.runtime = episode_count, runtime
        self.properties, self.thumb_url = properties, thumb_url
    
    def __hash__(self):
        return self.channel_id + self.time_start

    def __cmp__(self, other):
        return (self.channel_id + self.time_start) - (other.channel_id + other.time_start)

class EventList(list):
    """
    lista wydarzeń wraz z odpowiadającym jej kanałem
    """
    
    parser = None
    date = None
    channel = None
    
    def __init__(self, parser, date, channel, *args, **kwargs):
        self.parser, self.date, self.channel = parser, date, channel
        super(EventList, self).__init__(*args, **kwargs)